<?php

class Admin_ClientController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
        $this->list_sess_unset('controller_client');
        $this->session_search_key = 'controller_client';
        $this->session_search_name = 'search_label';
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'client', 'admin');
    }
	
	public function unsetsessionAction() {
        $pagename = $this->_getParam('pagename');
        unset($_SESSION['_LISTING_SESS']);
        $this->_helper->redirector($pagename, 'client', 'admin');
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $searchtype = "";
        $orderField = 'clnt_id';
        $sort = 'asc';

        if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) {
            $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = "";
        }
        /* Pagination Login */
        $itemsPerPageReviews = $this->PAGING_PER_PAGE;
        $currentPageReviews = 1;
		
        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }

        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();

            //Multiple Delete Logic 
            if (isset($postedData["multiaction"]) && ($postedData["multiaction"] != "")) {
                $action = trim($postedData["multiaction"]);
                if (isset($postedData["multicheck"]) && count($postedData["multicheck"]) > 0) {
                    $ids = implode(",", $_POST["multicheck"]);
                    $result = $this->deleteAction($_POST["multicheck"]);
                } else {
                    $errorMessage[] = "Please select one checkbox";
                }
            }
			/* Search Facility Over Here */
             if (isset($postedData["searchtype"]) && ($postedData["searchtype"] == "")) {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = "";
            }
            if (isset($postedData["searchtype"]) && ($postedData["searchtype"] != "")) {
                $searchtype = $postedData["searchtype"];
                $where = "clnt_first_name LIKE '%$searchtype%' OR clnt_phone LIKE '%$searchtype%' OR clnt_email LIKE '%$searchtype%'";
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = $searchtype;
                $this->view->searchtype = $searchtype;
            }
        } else {

            if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'])) {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = '';
            }
			$searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'];
            $where = "clnt_first_name LIKE '%$searchtype%' OR clnt_phone LIKE '%$searchtype%' OR clnt_email LIKE '%$searchtype%'";
            $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = $searchtype;
            $this->view->searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'];
        }
        //Load client Model
        $clientModel = new Model_Client();
        $clientData = $clientModel->fetchClientData($where, $itemsPerPageReviews, $offset, $orderField, $sort);
        //Paginator logiq
        $paginator = new GTL_Paginator();

        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($clientModel->getClientCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'client', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);

        $paginator->paginate($link);
		$GTLOBJ = new GTL_Common();

        $this->view->gtlObj = $GTLOBJ;
        $this->view->paginator = $paginator;
        $this->view->clientData = $clientData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->statusArray = $clientModel->getStatusArray();
        $this->view->searchtype = $searchtype;
    }

    public function formAction() {
		//echo '<pre>';print_r($this->USER);exit;
        //Initialize
        $clnt_id = '';
        $successmessage = '';
        $errorMessageArray = Array();
        $data = array();
        $clientData = Array();
		$clnt_available_to_everyone = "";
        $this->view->mode = 'Add';

        //Load client Model
        $clientModel = new Model_Client();

        //Load client Form
        $clientForm = new Form_Admin_Client();

        //Load the status array for the select box in form 
        $statusArray = $clientModel->getStatusArray();
        $clientForm->clnt_status->addMultiOptions($statusArray);
		
		//Load the Client Type array for the select box in form 
        $clienttypeArray = $clientModel->getClienttypeArray();
        $clientForm->clnt_type->addMultiOptions($clienttypeArray);
		
		//Load the Client Source array for the select box in form 
        $clientsourceArray = $clientModel->getClientsourceArray();
        $clientForm->clnt_source->addMultiOptions($clientsourceArray);
		
		//Load company Model
        $companyModel = new Model_Company();
		
		//Load the Client Company array for the select box in form 
        $clientcompanyArray = $companyModel->fetchCompany();
        $clientForm->clnt_company->addMultiOptions($clientcompanyArray);
		
		//Load the Company Industry array for the select box in form 
        $companyindustryArray = $companyModel->getCompanyindustryArray();
		
		//Load the Company Type array for the select box in form 
        $companytypeArray = $companyModel->getCompanytypeArray();

        //Check for the team_id Parameter
        $clientData['clnt_id'] = $this->_request->getParam('clnt_id');
        $clnt_id = (!empty($clientData['clnt_id'])) ? $clientData['clnt_id'] : null;

        if (!empty($clnt_id)) {

            //Set the Mode to Edit
            $this->view->mode = 'Edit';
            //Fetch data to be loaded in form 
            $data = $clientModel->fetchEntryById($clnt_id); 
			$clnt_available_to_everyone = $data['clnt_available_to_everyone'];
        }

        //Populate form 
        $this->view->form = $clientForm->populate($data);
		//echo '<pre>';print_r($data);exit;

        if ($this->getRequest()->isPost()) {
            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$clientForm->isValid($postedData)) {
                $errorMessage = $clientForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
               
                if (count($errorMessageArray) > 0) {
                    
                } else {

					if ($this->view->mode == 'Add') {
                        $clientData['clnt_createddate'] = date('Y-m-d H:i:s');
						$clientData['clnt_addedby'] = $this->USER->lgn_id;
                    }
                    //vat save
                    $clientData['clnt_id'] = $clnt_id;
                    $clientData['clnt_first_name'] = $postedData['clnt_first_name'];
                    $clientData['clnt_middle_name'] = $postedData['clnt_middle_name'];
					$clientData['clnt_last_name'] = $postedData['clnt_last_name'];
					$clientData['clnt_dob'] = $postedData['clnt_dob'];
                    $clientData['clnt_email'] = $postedData['clnt_email'];
					$clientData['clnt_phone'] = $postedData['clnt_phone'];
					$clientData['clnt_site'] = $postedData['clnt_site'];
					$clientData['clnt_company'] = $postedData['clnt_company'];
                    $clientData['clnt_position'] = $postedData['clnt_position'];
					$clientData['clnt_comment'] = $postedData['clnt_comment'];
					$clientData['clnt_adrline1'] = $postedData['clnt_adrline1'];
                    $clientData['clnt_adrline2'] = $postedData['clnt_adrline2'];
					$clientData['clnt_adrline3'] = $postedData['clnt_adrline3'];
					$clientData['clnt_street'] = $postedData['clnt_street'];
                    $clientData['clnt_postal'] = $postedData['clnt_postal'];
					$clientData['clnt_city'] = $postedData['clnt_city'];
					$clientData['clnt_country'] = $postedData['clnt_country'];
					if (array_key_exists('clnt_available_to_everyone', $postedData)) {
						$clientData['clnt_available_to_everyone'] = $postedData['clnt_available_to_everyone'];
					} else {
						$clientData['clnt_available_to_everyone'] = 0;
					}
					$clientData['clnt_type'] = $postedData['clnt_type'];
					$clientData['clnt_responsible'] = $this->USER->lgn_id;
                    $clientData['clnt_source'] = $postedData['clnt_source'];
					$clientData['clnt_description'] = $postedData['clnt_description'];
					$clientData['clnt_status'] = $postedData['clnt_status'];
					$clientData['clnt_updatedby'] = $this->USER->lgn_id;
                    $clientData['clnt_updateddate'] = date('Y-m-d H:i:s');
						//echo '<pre>';print_r($clientData);exit;
                    $clientModel->save($clientData, 'clnt_id');
                    if ($this->view->mode == 'Add') {
                        $successmessage = 'Client add successfully';
                    } else {
                        $successmessage = 'Client updated successfully';
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'client', 'admin');
                }
            }
        }
        $this->view->clntid = $clnt_id;
		$this->view->clnt_available_to_everyone = $clnt_available_to_everyone;
		//echo $clnt_available_to_everyone; exit;
        $this->view->errorMessageArray = $errorMessageArray;
		$this->view->companyindustryArray = $companyindustryArray;
		$this->view->companytypeArray = $companytypeArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $clientForm;
    }
	
	public function addcompanyAction() {
		 
		$comp_id = "";
		 
		//Load company Model
        $companyModel = new Model_Company();

        if ($this->getRequest()->isPost()) {
            $postdata = $this->_request->getPost();
            $companyData = array();
                    $companyData['comp_id'] = $comp_id;
					$companyData['comp_name'] = $postdata['comp_name'];
                    $companyData['comp_type'] = $postdata['comp_type'];
                    $companyData['comp_industry'] = $postdata['comp_industry'];
                    $companyData['comp_email'] = $postdata['comp_email'];
					$companyData['comp_phone'] = $postdata['comp_phone'];
					$companyData['comp_adrline1'] = $postdata['comp_adrline1'];
					$companyData['comp_addedby'] = $this->USER->lgn_id;
					$companyData['comp_updatedby'] = $this->USER->lgn_id;
					$companyData['comp_responsible_person'] = $this->USER->lgn_id;
					$companyData['comp_createddate'] = date('Y-m-d H:i:s');
                    $companyData['comp_updateddate'] = date('Y-m-d H:i:s');
                    $companyData['comp_status'] = 1;
					$company_last_id = $companyModel->save($companyData, 'comp_id');
              
            echo $company_last_id;
        }
        exit;
		 
	}
	 
	public function getcompanylistAction() {
		
        // $areaString = "<option>-- Select Area--</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
			
			$companyModel = new Model_Company();
			$CompanyArray = $companyModel->fetchCompany();
			
			if (count($CompanyArray) > 0) {
				$companyString = "";
				$selected = "";
				foreach ($CompanyArray as $comp_id => $comp_name) {
					
					if ($postedData['company_last_id'] == $comp_id){
						$selected = "selected";
					}
					
					$companyString .= "<option " . $selected . " value='" . $comp_id . "'>" . $comp_name . "</option>";
				}
			}        
        }
        echo $companyString;
        exit;
    }

    public function deleteAction($ids = Array()) {
		
		//Load team Model
        $clientModel = new Model_Client();
		
        if ($this->_getParam('clnt_id') && $this->_getParam('clnt_id') != '') {
            $clnt_id = $this->_getParam('clnt_id');

            $clientData = array();
            $clientData = $clientModel->fetchEntryById($clnt_id);
            
            $clientModel->deleteClient($clnt_id);
            $this->addSuccessMessage('Client Successfully Deleted');
            $this->_helper->redirector('list', 'client', 'admin');
        } else {
            $clientModel->deleteClient($ids);
            $this->addSuccessMessage('Client Successfully Deleted');
            $this->_helper->redirector('list', 'client', 'admin');
        }
    }

}

?>